/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package dao;

import common.JavaBean.Product;
import common.utils.DBUtilsV2;

import java.math.BigDecimal;
import java.sql.*;

public class ProductDao {

    /* 新增商品（管理员） */
    public int addProduct(String name, BigDecimal price, String desc, String imgUrl) throws SQLException {
        String sql = "{CALL sp_admin_add_product(?, ?, ?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setString(1, name);
            cstmt.setBigDecimal(2, price);
            cstmt.setString(3, desc);
            cstmt.setString(4, imgUrl);
            cstmt.registerOutParameter(5, Types.INTEGER);
            cstmt.execute();
            return cstmt.getInt(5);
        }
    }

    /* 多模态查询（管理员+用户共用） */
    public ResultSet queryProducts(Integer productId, String keyword,
                                   BigDecimal minPrice, BigDecimal maxPrice,
                                   Integer status) throws SQLException {
        String sql = "{CALL sp_query_products(?, ?, ?, ?, ?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        if (productId == null) cstmt.setNull(1, Types.INTEGER); else cstmt.setInt(1, productId);
        cstmt.setString(2, keyword);
        cstmt.setBigDecimal(3, minPrice);
        cstmt.setBigDecimal(4, maxPrice);
        if (status == null) cstmt.setNull(5, Types.TINYINT); else cstmt.setInt(5, status);
        return cstmt.executeQuery(); // 调用者负责关闭 ResultSet/Connection
    }

    public ResultSet askProducts(Integer productId, String name,
                                   BigDecimal Price,
                                   Integer status,String dtext) throws SQLException {
        String sql = "{CALL sp_ask_product_admin(?, ?, ?, ?, ?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        if (productId == null) cstmt.setNull(1, Types.INTEGER); else cstmt.setInt(1, productId);
        cstmt.setString(2, name);
        cstmt.setBigDecimal(3, Price);
        if(status ==null) cstmt.setNull(4, Types.TINYINT); else cstmt.setInt(4, status);
        if (dtext == null) cstmt.setNull(5, Types.VARCHAR); else cstmt.setString(5, dtext);
        return cstmt.executeQuery(); // 调用者负责关闭 ResultSet/Connection
    }

    /* 多模态修改（管理员） */
    public void updateProduct(int productId, String newName, BigDecimal newPrice,
                              String newDesc, String newImgUrl, Integer newStatus) throws SQLException {
        String sql = "{CALL sp_admin_update_product(?, ?, ?, ?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, productId);
            cstmt.setString(2, newName);
            cstmt.setBigDecimal(3, newPrice);
            cstmt.setString(4, newDesc);
            cstmt.setString(5, newImgUrl);
            if (newStatus == null) cstmt.setNull(6, Types.TINYINT); else cstmt.setInt(6, newStatus);
            cstmt.execute();
        }
    }
}